<?php
namespace app\service;
use think\Db;

class wechatService{
    
	//查询是否为月租车或地库车
    public function numberExemption($car_number){
        
        $package = Db::name('package')->where('car_number',$car_number)->find();
        if(empty($package)){
			$library=Db::name('library')->where('car_number',$car_number)->find();
			if(empty($library)){
				return $library;
			}else{
				$package["car_type"]="地库车";
				return $library;
			}
        }else{
			$package["car_type"]="月租车";
            return $package;
        }
    }
	
    public function post_curl($url){
        $curl = curl_init();//启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url);//要访问的地址
        curl_setopt($curl, CURLOPT_HEADER, 1);//设置header
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//获取的信息以文件流的形式返回
        curl_setopt($curl, CURLOPT_TIMEOUT, 30);//设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//跳过ssl检查项
        curl_setopt($curl, CURLOPT_HEADER, 0);//显示返回的Header区域内容
        $tmpInfo = curl_exec($curl);//执行操作
        // $err = curl_error($curl);//输出错误
        curl_close($curl);//关闭CURL会话
        return $tmpInfo;
    }
    //
    public function post_curl_data($url,$xml){
        //CURL模拟请求
        $curls = curl_init(); //启动一个CURL会话
        curl_setopt($curls, CURLOPT_URL, $url); //要访问的地址
        curl_setopt($curls, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curls, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curls, CURLOPT_HEADER, FALSE);//设置header
        curl_setopt($curls, CURLOPT_RETURNTRANSFER, TRUE);//要求结果为字符串且输出到屏幕上
        curl_setopt($curls, CURLOPT_POST, TRUE); //发送一个常规的Post请求
        curl_setopt($curls, CURLOPT_POSTFIELDS, $xml); //Post提交的数据包
        curl_setopt($curls, CURLOPT_TIMEOUT, 30); //设置超时限制防止死循环
        $payInfo = curl_exec($curls); //执行操作
         //$err = curl_error($curls);//输出错误
        curl_close($curls); //关闭CURL会话
        return $payInfo;
    }

    //生成签名
    public function MakeSign($arr,$wxkey){
        //签名步骤一：按字典序排序参数
        ksort($arr);
        $string = $this->ToUrlParams($arr);
        //签名步骤二：在string后加入KEY
        $string = $string . "&key=" . $wxkey;
        //签名步骤三：MD5加密
		//$string = md5($string);
		
		$string=hash_hmac('sha256',$string, $wxkey);
        //签名步骤四：所有字符转为大写
        $result = strtoupper($string);
        return $result;
    }
    //格式化参数格式化成url参数
    public function ToUrlParams($value){
        $buff = "";
        foreach($value as $k => $v){
            if($k != "sign" && $v != "" && !is_array($v)){
                $buff .= $k . "=" . $v . "&";
            }
        }
        $buff = trim($buff, "&");
        return $buff;
    }
    //生成签名
    public function MakeSigns($arr,$wxkey){
        //签名步骤一：按字典序排序参数
        ksort($arr);
        $string = $this->ToUrlParams($arr);
        //签名步骤二：在string后加入KEY
        $string = $string . "&key=" . $wxkey;
        //签名步骤三：MD5加密
        $result = md5($string);
        return $result;
    }
    //产生随机字符串，不长于32位
    public function createNoncestr($length = 32){
        $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++)
        {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }

        return $str;
    }
    //curl
    
        
	//将对象转换成数组
    public function object_to_array($obj){
        $_arr = is_object($obj) ? get_object_vars($obj) :$obj;
        foreach($_arr as $key=>$val){
            if($key !== 'gioouts'){
                $val = (is_array($val) || is_object($val)) ? $this->object_to_array($val):$val;
                $arr[$key] = $val;
            }
        }
        return $arr;
    }
    public function ToXml($arr){
        $xml = "<xml>";
        foreach ($arr as $key => $val){
            if (is_numeric($val)){
                $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
            }else{
                $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
            }
        }
        $xml .= "</xml>";
        return $xml;
    }
}